home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4059 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.9 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Got Questions About Multi-Dimension Arrays (After Reading FAQ)
  5. Date: 01 Feb 1996 16:29:11 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Feb1092911@qcd.lanl.gov>
  9. References: <4ep87b$o60@alcor.usc.edu>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: wawda@alcor.usc.edu's message of 31 Jan 1996 18:23:39 -0800
  14.  
  15. In article <4ep87b$o60@alcor.usc.edu> wawda@alcor.usc.edu (Abu Wawda)
  16. writes: 
  17. <snip>
  18.    I read questions 6.19 and 6.20 from the FAQ that deal with
  19.    multi-dimensional arrays but am still confused. If someone can clear
  20.    up my confusion, I would really appreciate it. Bascailly I still don't
  21.    see why it isn't possible to do this:
  22.  
  23.    void myfunc(int *array,int rows,int cols)
  24.    {
  25.       printf("%d\n",array[1][1]);
  26.    } 
  27.  
  28. You cannot do it because an int cannot be subscripted with another
  29. int. Consider that after int *array, array[1] is an int. So, what do
  30. you expect array[1][1] to be? What you have written is similar to
  31. writing int x; and then writing x[1] ... if you think about it, you
  32. will see that no natural interpretation is possible for this.
  33.  
  34. I think your basic confusion is that you are thinking of
  35. `multi-dimensional' arrays. It might help you if you realized that the
  36. concept you have in your mind does not exist in C. C does have arrays
  37. of arrays (and other concepts like arrays of pointers and pointers to
  38. pointers etc.) which most people think of the same way as
  39. multi-dimensional arrays: but in understanding the nature of C
  40. operators, it is best to think of them as such. Thus array[1][2] (I am
  41. using [1][2] instead of [1][1] to avoid trivial confusion) is not the
  42. elementary operation take the 1,2 th element of array, but rather the
  43. operation of taking the second element from the first element of
  44. array. So, if the 1st element of array is not an array or a pointer to
  45. the first element of an array, taking its second element is a bit
  46. difficult :-)
  47.  
  48. From your later question, it is clear that you do not mean int**
  49. instead of int* above: I bring that up becuase after int**, you can
  50. indeed write array[1][2] etc. But, remember that in this case, array
  51. is not an array of arrays of ints , but rather a pointer to the first
  52. element of an array of pointers to the first elements of arrays of
  53. ints. Note that that an array of arrays of ints means that the arrays
  54. of ints are packed closely together into one array and that is all
  55. there is to it; an array of pointers to the first elements of arrays
  56. of ints means that the individual arrays of ints are possibly
  57. scatterred all over memory and pointers to the first elements are
  58. stored closely packed into one array.
  59.  
  60.    and call the function like this:
  61.  
  62.    int m[2][2] = {1, 2, 3, 4};
  63.  
  64.    myfunc(m,2,2);
  65.  
  66. Because when one says that an array decays to a pointer, one does not
  67. mean that a multidimensional array decyas to a pointer. As I said,
  68. conceptually there is nothing called a multidimensional array: and an
  69. array or arrays decays to a pointer to an array, not a pointer to an
  70. int. 
  71.  
  72.    If I understand correctly, a multideminsional array is really stored
  73.  
  74. Let is call it array of arrays.
  75.  
  76.    continguously like this in memory:
  77.  
  78. Sure: an array of arrays is a contiguous sequence of arrays, each of
  79. which is a contiguous sequence of ints
  80.  
  81.    array[0][0]    | 1 | memory address a
  82.    array[0][1]    | 2 | memory address a+sizeof(int)
  83.    array[1][0]    | 3 | ....
  84.    array[1][1]    | 4 | ....
  85.  
  86.    So why can't C convert from pointer to integer to a multidemsional
  87.    array. The reason I want to do this is because my function needs to
  88.  
  89. Because there is _no_ multidimensional array in C. Sorry.
  90.  
  91. If you wanted a fixed dimension, then you could convert from an int *
  92. to a pointer to the first of a bunch of arrays. Thus
  93.  
  94.    void myfunc(int *array,int rows,int cols)
  95.    {
  96.       printf("%d\n",((int(*)[2])array)[1][1]);
  97.    } 
  98.  
  99. followed by
  100.  
  101.    int m[2][2] = {1, 2, 3, 4};
  102.  
  103.    myfunc((int*)m,2,2);
  104.  
  105. is indeed valid.  The problem is that a _type_ in C cannot contain
  106. variables (and that rule goes very deep in C: a type is a compile time
  107. constant), and so you cannot have arrays of truly unknown dimension
  108. (that would be a variable type). So, the analogous expression
  109. (int(*)[cols])array is not C.
  110.  
  111.    take in an array of any width and height. Thank you for your time,
  112.  
  113. The FAQ discusses the pros and cons of many of the methods there. If
  114. my answer is still not clear, please post further followups. 
  115.  
  116. Cheers
  117. Tanmoy
  118. --
  119. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  120. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  121. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  122. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  123. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  124. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  125.